<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\ProductCate;
use think\facade\Cache;
use think\facade\View;
use think\response\Json;
use app\common\model\Product as ProductModel;

class Product extends Base
{

    public function index()
    {
        $cate_1 = $this->request->param('cate_1');
        $keyword = $this->request->param('keyword');
        $cate = ProductCate::where('id', $cate_1)->value('title');
        View::assign([
            'title' => $keyword ?: $cate,
            'cate_1' => $cate_1,
            'keyword' => $keyword
        ]);
        return View::fetch();
    }

    public function search(): Json
    {
        $history = Cache::get('searchHistory_' . $this->user->id);
        if ($history) {
            $history = json_decode($history, true);
            $news = [];
            for ($i = count($history); $i > 0; $i--) {
                $news[] = $history[$i - 1];
            }
            $history = $news;
        }
        return json(['code' => 1, 'msg' => 'success', 'result' => $history]);
    }

    public function emptySearch()
    {
        Cache::delete('searchHistory_' . $this->user->id);
        return json(['code' => 1, 'msg' => '已清空']);
    }

    public function details($id)
    {
        $info = ProductModel::where(['status' => 1, 'is_del' => 0, 'id' => $id])
            ->field('id,title,describe,market_price,price,icon,stock,sales,content,images')->find();
        if (empty($info)) return json(['code' => -1, 'msg' => '商品不存在或已下架']);
        $url = 'https://' . $_SERVER['SERVER_NAME'] . '/uploads/';
        $info->content = htmlspecialchars_decode(str_replace('/uploads/', $url, $info->content));
        $info->content = str_replace('\\', '', $info->content);

        $info->images = explode(',', $info->images);
        $info->images = handleImg($info->images);

        return json(['code' => 1, 'msg' => 'success', 'result' => $info]);
    }

    public function getList(): Json
    {
        $param = $this->request->param();
        $map = [];
        foreach ($param as $key => $value) {
            if (!in_array($key, ['page', 'type', 'keyword']) && $value) {
                $map[$key] = $value;
            }
        }
        $like = [];
        if (isset($param['keyword']) && $param['keyword']) {
            $like['field'] = 'title';
            $like['keyword'] = $param['keyword'];
            if(isset($this->user)){
                $history = Cache::get('searchHistory_' . $this->user->id);
                if ($history) {
                    $history = json_decode($history, true);
                    if (!in_array($param['keyword'], $history)) {
                        $history[] = $param['keyword'];
                        Cache::set('searchHistory_' . $this->user->id, json_encode($history));
                    }
                } else {
                    $history[] = $param['keyword'];
                    Cache::set('searchHistory_' . $this->user->id, json_encode($history));
                }
            }

        }
        return json(ProductModel::getPageList($map, 'id,title,price,icon', $param['page'], 20, '', '', $like));
    }

    public function submit()
    {
        $param = $this->request->post();
        $result = ProductModel::submit($param, $this->user);
        $result['code'] = $result['code'] >= 1 ? 1 : -1;
        return json($result);
    }
}

